﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using EIA.Basic.Model;

namespace EIA.SLClient.PlatManage.BasicMgr
{
    public class StaffListViewModel : SimpleMvvmToolkit.ViewModelBase<StaffListViewModel>
    {
        public StaffListViewModel()
        {
            this.RegisterToReceiveMessages(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.STAFF_RETRIEVE, EP_Retrieve);
            this.RegisterToReceiveMessages<Tuple<TStaff, TUser>>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.STAFF_NEW_CANCEL, EP_NewCancel);
        }

        private void EP_Retrieve(object sender, SimpleMvvmToolkit.NotificationEventArgs e)
        {
            RetrieveItems();
        }
        private void EP_NewCancel(object sender, SimpleMvvmToolkit.NotificationEventArgs<Tuple<TStaff, TUser>> e)
        {
            if (e.Data != null && StaffCollection != null) StaffCollection.Remove(e.Data);
        }


        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.STAFF_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.STAFF_BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected PagedCollectionView _StaffCollection;
        /// <summary>
        /// 注释
        /// </summary>
        public PagedCollectionView StaffCollection
        {
            get { return _StaffCollection; }
            set
            {
                _StaffCollection = value;
                NotifyPropertyChanged(m => m.StaffCollection);
                OnFilter();
            }
        }

        protected bool _ShowDeactive;
        /// <summary>
        /// 是否显示注销用户
        /// </summary>
        public bool ShowDeactive
        {
            get { return _ShowDeactive; }
            set
            {
                _ShowDeactive = value;
                NotifyPropertyChanged(m => m.ShowDeactive);
                OnFilter();
            }
        }

        private void OnFilter()
        {

            if (StaffCollection != null)
            {
                if (ShowDeactive)
                    StaffCollection.Filter = null;
                else
                    StaffCollection.Filter = (staff) => { return (staff as Tuple<TStaff, TUser>).Item1.IsActive != 0; };
            }
        }

        #endregion

        #region command

        #region RetrieveItemsCommand

        private SimpleMvvmToolkit.DelegateCommand _RetrieveItemsCommand = null;
        public SimpleMvvmToolkit.DelegateCommand RetrieveItemsCommand
        {
            get
            {
                if (_RetrieveItemsCommand == null)
                {
                    _RetrieveItemsCommand = new SimpleMvvmToolkit.DelegateCommand(RetrieveItems, CanExcuteRetrieveItems);
                } return _RetrieveItemsCommand;
            }
            set { _RetrieveItemsCommand = value; NotifyPropertyChanged(m => m.RetrieveItemsCommand); }
        }

        public void RetrieveItems()
        {

            EIA.Basic.BLL.ClientProxy.XStaffProxy proxy = new Basic.BLL.ClientProxy.XStaffProxy();
            Busy();

            Dictionary<string, object> con = new Dictionary<string, object>();
            //con[TStaff.IS_ACTIVE] = 1;

            proxy.List(con, int.MaxValue, 1, (lst) =>
            {
                Free();

                StaffCollection = new PagedCollectionView(lst);
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("获取员工列表发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
            //List<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>> lst = new List<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>>();
            //for (int i = 0; i < 10; i++)
            //{
            //    lst.Add(new Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>(
            //        new Basic.Model.TStaff() { StfName = "职员" + i, DepName = "部门A", ContactTel = "13012345678", PosName = "职位1", StfId = i, IsActive = 1 },
            //        new Basic.Model.TUser() { StfId = i, LoginAvalible = 1, LoginId = "user" + i, LoginPwd = "pwd" + i }
            //        ));
            //}

            //StaffCollection = new PagedCollectionView(lst);
        }

        public bool CanExcuteRetrieveItems()
        {
            return true;
        }

        #endregion

        #region InsertItemCommand

        private SimpleMvvmToolkit.DelegateCommand _InsertItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand InsertItemCommand
        {
            get
            {
                if (_InsertItemCommand == null)
                {
                    _InsertItemCommand = new SimpleMvvmToolkit.DelegateCommand(InsertItem, CanExcuteInsertItem);
                } return _InsertItemCommand;
            }
            set { _InsertItemCommand = value; NotifyPropertyChanged(m => m.InsertItemCommand); }
        }

        public void InsertItem()
        {
            Tuple<TStaff, TUser> staff_user = new Tuple<TStaff, TUser>(new TStaff(), new TUser());
            staff_user.Item1.StfName = "新员工";
            staff_user.Item1.IsActive = 1;
            staff_user.Item2.LoginAvalible = 1;

            List<Tuple<TStaff, TUser>> lst = new List<Tuple<TStaff, TUser>>();
            lst.Add(staff_user);
            lst.AddRange(StaffCollection.SourceCollection as IList<Tuple<TStaff, TUser>>);
            StaffCollection = new PagedCollectionView(lst);
            this.SendMessage<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.STAFF_NEW, new SimpleMvvmToolkit.NotificationEventArgs<Tuple<TStaff, TUser>>("", staff_user));
        }

        public bool CanExcuteInsertItem()
        {
            return true;
        }

        #endregion

        #region UpdateItemCommand

        private SimpleMvvmToolkit.DelegateCommand<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>> _UpdateItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>> UpdateItemCommand
        {
            get
            {
                if (_UpdateItemCommand == null)
                {
                    _UpdateItemCommand = new SimpleMvvmToolkit.DelegateCommand<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>>(UpdateItem, CanExcuteUpdateItem);
                } return _UpdateItemCommand;
            }
            set { _UpdateItemCommand = value; NotifyPropertyChanged(m => m.UpdateItemCommand); }
        }

        public void UpdateItem(Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser> p)
        {
            this.SendMessage<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>>(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.STAFF_UPDATE, new SimpleMvvmToolkit.NotificationEventArgs<Tuple<TStaff, TUser>>("", p));
        }

        public bool CanExcuteUpdateItem(Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser> p)
        {
            return true;
        }

        #endregion

        #region DeleteItemCommand

        private SimpleMvvmToolkit.DelegateCommand<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>> _DeleteItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>> DeleteItemCommand
        {
            get
            {
                if (_DeleteItemCommand == null)
                {
                    _DeleteItemCommand = new SimpleMvvmToolkit.DelegateCommand<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>>(DeleteItem, CanExcuteDeleteItem);
                } return _DeleteItemCommand;
            }
            set { _DeleteItemCommand = value; NotifyPropertyChanged(m => m.DeleteItemCommand); }
        }

        public void DeleteItem(Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser> p)
        {
            // 未使用 所以未实现
        }

        public bool CanExcuteDeleteItem(Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser> p)
        {
            return true;
        }

        #endregion

        #region DeactiveCommand

        private SimpleMvvmToolkit.DelegateCommand<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>> _DeactiveCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>> DeactiveCommand
        {
            get
            {
                if (_DeactiveCommand == null)
                {
                    _DeactiveCommand = new SimpleMvvmToolkit.DelegateCommand<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>>(Deactive, CanExcuteDeactive);
                } return _DeactiveCommand;
            }
            set { _DeactiveCommand = value; NotifyPropertyChanged(m => m.DeactiveCommand); }
        }

        public void Deactive(Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser> p)
        {
            if (System.Windows.MessageBox.Show(string.Format("确认注销员工 [{0}]", p.Item1.StfName), "提示", System.Windows.MessageBoxButton.OKCancel) != System.Windows.MessageBoxResult.OK) return;

            EIA.Basic.BLL.ClientProxy.XStaffProxy proxy = new Basic.BLL.ClientProxy.XStaffProxy();
            Busy();
            proxy.Deactive(p.Item1.StfId, (r) =>
            {
                Free();
                if (r)
                {
                    p.Item1.IsActive = 0;
                    if (p.Item2 != null) p.Item2.LoginAvalible = 0;
                    StaffCollection.Refresh();
                    //SendMessage(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.STAFF_RETRIEVE, new SimpleMvvmToolkit.NotificationEventArgs());
                }
                else
                {
                    // 未注销
                    System.Windows.MessageBox.Show("注销员工失败！", "提示", System.Windows.MessageBoxButton.OK);
                }

            }, (ex) =>
            {
                Free();
            });
        }

        public bool CanExcuteDeactive(Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser> p)
        {
            return true;
        }

        #endregion

        #region ActiveCommand

        private SimpleMvvmToolkit.DelegateCommand<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>> _ActiveCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>> ActiveCommand
        {
            get
            {
                if (_ActiveCommand == null)
                {
                    _ActiveCommand = new SimpleMvvmToolkit.DelegateCommand<Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser>>(Active, CanExcuteActive);
                } return _ActiveCommand;
            }
            set { _ActiveCommand = value; NotifyPropertyChanged(m => m.ActiveCommand); }
        }

        public void Active(Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser> p)
        {
            if (System.Windows.MessageBox.Show(string.Format("确认恢复员工 [{0}]", p.Item1.StfName), "提示", System.Windows.MessageBoxButton.OKCancel) != System.Windows.MessageBoxResult.OK) return;

            EIA.Basic.BLL.ClientProxy.XStaffProxy proxy = new Basic.BLL.ClientProxy.XStaffProxy();
            Busy();
            proxy.Active(p.Item1.StfId, (r) =>
            {
                Free();
                if (r)
                {
                    p.Item1.IsActive = 1;
                    if (p.Item2 != null) p.Item2.LoginAvalible = 1;
                    StaffCollection.Refresh();
                    //SendMessage(EIA.SLClient.PlatManage.Infrastructure.MessageTokens.STAFF_RETRIEVE, new SimpleMvvmToolkit.NotificationEventArgs());
                }
                else
                {
                    System.Windows.MessageBox.Show("恢复员工失败！", "提示", System.Windows.MessageBoxButton.OK);
                }

            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("恢复员工发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteActive(Tuple<EIA.Basic.Model.TStaff, EIA.Basic.Model.TUser> p)
        {
            return true;
        }

        #endregion

        #endregion

        public void OnLoad()
        {
            if (StaffCollection == null) RetrieveItems();
        }
    }
}